const jwt = require('jsonwebtoken')
const expiresDay = 1000 * 60 * 60 * 24 * 7;//7天
const secretKey = 'PTF';//密钥

class JWTUtil {
    /**
     * 生成一个7天有效期的token
     */
    createToken(payload) {
        payload.iat = Date.now();
        payload.exp = payload.iat + expiresDay;
        return jwt.sign(payload, secretKey)
    }

    /**
     * 校验token并返回用户信息
     */
    verifyToken(token) {
        return jwt.verify(token, secretKey, (err, authData) => {
            if (err) {
                err.status = 401;
                throw err;
            } else {
                return authData;
            }
        });
    }
}

module.exports = new JWTUtil();